const friendService = require('../service/friend.service')

class FriendController {
  // 发送好友申请
  async applyAdd(ctx, next) {
    console.log('hello')
    const userId = ctx.user.id;
    const {f_id, f_name, u_name} = ctx.request.body
    if(userId === f_id) return
    //数据库添加两条数据，state不同
    await friendService.applyAdd(userId, f_id, f_name, 2)
    await friendService.applyAdd(f_id, userId, u_name, 1)
    ctx.success({
      msg: '已发送添加请求'
    })
  }
  // 获取好友申请列表
  async applyList(ctx, next) {
    const userId = ctx.user.id
    const result = await friendService.getApplyListById(userId)
    ctx.success({
      data: result
    })
  }
  // 同意好友申请
  async agree(ctx, next) {
    const userId = ctx.user.id
    const {f_id} = ctx.request.body;
    await friendService.agreeApply(userId, f_id)
    await friendService.agreeApply(f_id, userId)
    ctx.success({
      msg: '已同意申请'
    })
  }
  // 获取好友列表
  async friendList(ctx, next) {
    const userId = ctx.user.id;
    const result = await friendService.getFriendListById(userId)
    ctx.success({
      data: result
    })
  }
  // 删除好友，拒绝申请
  async remove(ctx, next) {
    const userId = ctx.user.id;
    const {f_id} = ctx.request.body;
    await friendService.removeFriend(userId, f_id)
    await friendService.removeFriend(f_id, userId)
    ctx.success({
      msg: '删除成功'
    })
  }
  // 修改好友备注
  async rename(ctx, next) {
    const userId = ctx.user.id;
    const {f_id, newName} = ctx.request.body;
    await friendService.rename(userId, f_id, newName)
    ctx.success({
      msg: '修改备注成功'
    })
  }
}

module.exports = new FriendController()